package com.hemerasolutions.v2trainer.model.multimedia;

import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Transient;

import org.primefaces.model.UploadedFile;

import com.hemerasolutions.v2trainer.model.base.BaseModel;
import com.hemerasolutions.v2trainer.model.interfaces.BaseModelInterface;

@Entity
public class RemoteVideo extends BaseModel implements BaseModelInterface {

	private static final long serialVersionUID = 1L;

    @Column(length = 50)
	private String storageId;
    
    @Column(length = 64,unique=true)
	private String videoHascode;

	@Column
	private Long uuid;	
    
    @Transient
    private UploadedFile uploadedFile;

    
    public static final long DEFAULT_EXERCISE_VIDEO_UUID = 123456;
    public static final long DEFAULT_EQUIPMENT_VIDEO_UUID = 78910;     
    
    
	public RemoteVideo() {
		super();
	}


	public RemoteVideo(RemoteVideo other) {
		super();
		this.id = other.id;
		this.storageId = other.storageId;
		this.videoHascode = other.videoHascode;
		this.uploadedFile = other.uploadedFile;
		this.uuid = UUID.randomUUID().getMostSignificantBits();;
	}


	public String calcVideoMD5HashCode(){
        return org.apache.commons.codec.digest.DigestUtils.md5Hex(this.getUploadedFile().getContents());
	}
    
    
	public String getStorageId() {
		return storageId;
	}

	public UploadedFile getUploadedFile() {
		return uploadedFile;
	}

	public void setUploadedFile(UploadedFile uploadedFile) {
		this.uploadedFile = uploadedFile;
		this.videoHascode = this.calcVideoMD5HashCode();
	}

	public void setStorageId(String storageId) {
		this.storageId = storageId;
	}

	public String getVideoHascode() {
		return videoHascode;
	}

	public void setVideoHascode(String videoHascode) {
		this.videoHascode = videoHascode;
	}

	public Long getUuid() {
		if(this.uuid == null){
			this.uuid = UUID.randomUUID().getMostSignificantBits();
		}
		return uuid;
	}

	public void setUuid(Long uuid) {
		this.uuid = uuid;
	}


	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((storageId == null) ? 0 : storageId.hashCode());
		result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
		result = prime * result
				+ ((videoHascode == null) ? 0 : videoHascode.hashCode());
		return result;
	}


	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		RemoteVideo other = (RemoteVideo) obj;
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (storageId == null) {
			if (other.storageId != null) {
				return false;
			}
		} else if (!storageId.equals(other.storageId)) {
			return false;
		}
		if (uuid == null) {
			if (other.uuid != null) {
				return false;
			}
		} else if (!uuid.equals(other.uuid)) {
			return false;
		}
		if (videoHascode == null) {
			if (other.videoHascode != null) {
				return false;
			}
		} else if (!videoHascode.equals(other.videoHascode)) {
			return false;
		}
		return true;
	}


	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "RemoteVideo [id=" + id + ", storageId=" + storageId
				+ ", videoHascode=" + videoHascode + ", uuid=" + uuid + "]";
	}




    
}
